import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {RoleInfoService} from "../service/role-info.service";
import {NzMessageService} from "ng-zorro-antd/message";
import {RoleMenu} from "../entity/role-menu";
import {RoleMenuService} from "../../services/role-menu.service";

@Component({
  selector: 'app-role-menus',
  templateUrl: './role-menus.component.html',
  styleUrls: ['./role-menus.component.less']
})
export class RoleMenusComponent implements OnInit {

  roleId: string
  roleName: string

  menus: RoleMenu[] = []
  menuMap: { [pid: string]: RoleMenu }

  loading = false

  constructor(private route: ActivatedRoute, private roleService: RoleInfoService, private msg: NzMessageService,
              private roleMenuService: RoleMenuService) {
  }

  ngOnInit(): void {

    this.roleId = this.route.snapshot.queryParams['pid']
    this.roleName = this.route.snapshot.queryParams['roleName']
    this.search()
  }


  search() {
    this.loading = true
    const param = new RoleMenu()
    param.roleId = this.roleId
    this.roleService.getMenus(param)
      .subscribe(value => {
        this.menuListToMap(value)
        this.loading = false
      }, () => this.loading = false);

  }

  add(menuId: string) {
    this.loading = true
    this.roleMenuService.add(this.roleId, menuId).subscribe(() => {
      this.msg.success("保存成功")
      this.search()
    }, () => this.loading = false);
  }

  delete(menuId: string) {
    this.loading = true
    this.msg.success("删除成功")
    this.roleMenuService.delete(this.roleId, menuId).subscribe(() => this.search(), () => this.loading = false)
  }


  menuListToMap(menuList: RoleMenu[] = []) {
    if (!menuList.length) {
      return {}
    }
    const menuMap: { [pid: string]: RoleMenu } = {};
    const topMenuArray: RoleMenu[] = [];
    for (let i = 0; i < menuList.length; i++) {
      menuList[i].expand = true
      menuMap[menuList[i].menuId] = menuList[i];
      if (!menuList[i].parentId) {
        topMenuArray.push(menuList[i])
      }
    }

    // 构造层级关系
    for (let i = 0; i < menuList.length; i++) {
      if (menuList[i].parentId) {
        const parent = menuMap[menuList[i].parentId];
        if (parent) {
          if (!parent.children) {
            parent.children = [];
          }
          parent.children.push(menuList[i])
        } else {
          menuList[i].parentId = null
        }
      }
    }
    this.menuMap = menuMap

    //构造一个树顺序的array,按父子关系排序，就是树全展开的样子
    let result: RoleMenu[] = []
    for (let i = 0; i < topMenuArray.length; i++) {
      topMenuArray[i].level = 0
      result.push(topMenuArray[i]);
      result = result.concat(this.getAllChildren(topMenuArray[i], 1));
    }
    this.menus = result;
  }

  getAllChildren(parent: RoleMenu, level: number): RoleMenu[] {
    if (!parent.children || !parent.children.length) {
      return []
    }
    const children = parent.children;
    let result: RoleMenu[] = []
    for (let i = 0; i < children.length; i++) {
      children[i].level = level
      result.push(children[i])
      result = result.concat(this.getAllChildren(children[i], (level + 1)))
    }
    return result
  }

  isShow(menu: RoleMenu): boolean {
    // 父亲，爷爷，爷爷的爸爸...,是不是展开
    if (!menu.parentId) {
      return true
    } else {
      const parent = this.menuMap[menu.parentId];
      if (parent) {
        if (!parent.expand) {
          return false
        } else {
          return this.isShow(parent)
        }
      } else {
        return true
      }
    }
  }

  deleteConfirmMessage(menu: RoleMenu) {
    if (menu.children && menu.children.length) {
      return 'Are you sure delete this menu:' + menu.menuName + ',and it`s submenu ?'
    } else {
      return 'Are you sure delete this menu:' + menu.menuName + ' ?'
    }
  }
}
